package com.guli.ucenter.controller;


import com.guli.ucenter.entity.Member;
import com.guli.ucenter.entity.vo.LoginInfoVo;
import com.guli.ucenter.entity.vo.LoginVo;
import com.guli.ucenter.entity.vo.RegisterVo;
import com.guli.ucenter.service.MemberService;
import com.guli.utils.JwtUtils;
import com.guli.utils.R;
import com.guli.utils.UcenterMember;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ZhaoZhenyu
 * @since 2022-10-02
 */

//@CrossOrigin
@RestController
@RequestMapping("/ucenter/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    // 登录
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){

        String token = memberService.login(loginVo);

        return R.ok().data("token", token);

    }

    // 注册
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){

        memberService.register(registerVo);

        return R.ok();
    }

    // 根据token得到会员信息【id和nickname】
    @GetMapping("/auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        // request请求头里面带有token
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Member member = memberService.getById(memberId);
        LoginInfoVo loginInfoVo = new LoginInfoVo();
        BeanUtils.copyProperties(member,loginInfoVo);
        return R.ok().data("item", loginInfoVo);
    }

    //根据token字符串获取用户信息
    @PostMapping("getInfoUc/{id}")
    public UcenterMember getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        Member member = memberService.getById(id);
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(member,ucenterMember);
        return ucenterMember;
    }

    /**
     * 统计注册人数
     * @param day
     * @return
     */
    @GetMapping(value = "/countRegister/{day}")
    public Integer registerCount(@PathVariable String day){
        return memberService.countRegisterByDay(day);
    }

}

